// app.js
var api = require("/utils/request.js");
App({
  onLaunch() {

    var that = this
    wx.showShareMenu({
      withShareTicket:true
    })
    //
    const obj = wx.getLaunchOptionsSync()
    wx.setStorageSync('targetId', obj.query.id==null?444:obj.query.id)//335
    wx.setStorageSync('leaderId', obj.query.userId==null?30:obj.query.userId)
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log('===登录===' + res)
        wx.setStorageSync('code', res.code)
    
        wx.getSetting({
          success: res => {
            if (res.authSetting['scope.userInfo']) {
              // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
              wx.getUserInfo({
                success: res => {
                  // 可以将 res 发送给后台解码出 unionId
                  // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                  // 所以此处加入 callback 以防止这种情况
                  var that = this
                  console.log('res===='+res.userInfo.avatarUrl);
                  console.log('res===='+res.userInfo.nickName);
                  api.request1({"tuangouId":wx.getStorageSync('targetId'),"leaderId":wx.getStorageSync('leaderId'),"url":res.userInfo.avatarUrl,
                  "nickName":res.userInfo.nickName==null?"":res.userInfo.nickName, "code": wx.getStorageSync('code'), "path": "user/userLogin" }, function (result) {
                    if(result.code=='0'){
                      
                      wx.setStorageSync('id', result.data.id)
                      wx.setStorageSync('token', result.data.token)
                      wx.setStorageSync('nickName', result.data.nickName)
                      wx.setStorageSync('avatarUrl', result.data.headUrl)
                      wx.setStorageSync('phone', result.data.phone)
                      that.globalData.checkLogin = true;

                      if (that.checkLoginReadyCallback){
                        that.checkLoginReadyCallback(result);
                      }
                    }else if(result.code = '500'){
                      wx.showToast({
                        title: '当前团购无数据，请到团购微信群打开此小程序',
                        icon:"error"
                      })
                    } 
                  }, function (error) {
                    console.log("error===",error);
                  })
                }
              })
            }else{
              var that = this
              wx.getUserInfo({
                success: res => {
                  console.log(res.userInfo);
                  api.request1({"tuangouId":wx.getStorageSync('targetId'),"leaderId":wx.getStorageSync('leaderId'),"url":res.userInfo.avatarUrl,
                  "nickName":res.userInfo.nickName.length==0?"":res.userInfo.nickName, "code": wx.getStorageSync('code'), "path": "user/userLogin" }, function (result) {
           
                    if(result.code=='0'){
                      wx.setStorageSync('id', result.data.id)
                      wx.setStorageSync('token', result.data.token)
                      wx.setStorageSync('nickName', result.data.nickName)
                      wx.setStorageSync('avatarUrl', result.data.headUrl)
                      wx.setStorageSync('phone', result.data.phone)
                      that.globalData.checkLogin = true;
                      if (that.checkLoginReadyCallback){
                        that.checkLoginReadyCallback(result);
                      }else if(result.code = '500'){
                        wx.showToast({
                          title: '当前团购无数据，请到团购微信群打开此小程序',
                          icon:"error"
                        })
                      } 
                    }
                  }, function (error) {
                    console.log("error===",error);
                  })
                }
              })
            }
          }
        })
      }
    })
    wx.getSystemInfo({
      success: e => {
        this.globalData.windowHeight = e.windowHeight;
        this.globalData.statusBarHeight = e.statusBarHeight; 
      }
    })
    const res = wx.getMenuButtonBoundingClientRect();
    this.globalData.naviBarHeight = (res.top - this.globalData.statusBarHeight) * 2 + res.height
    this.globalData.zHeight = this.globalData.naviBarHeight + this.globalData.statusBarHeight
  },
  onShow(){
    this.globalData.isRelaunch=true
  },



  globalData: {
    checkLogin: false,
    version: "1.0.0",
    releaseUrl: "https://txetest.llmmwl.com:8686/txe/",
    debugUrl: "https://txetest.llmmwl.com:8686/txe/",
    debug: true,
    windowHeight :721,
    statusBarHeight :0,
    naviBarHeight : 0,
    zHeight :0,
    isCheck1 :0,
    isRelaunch:false
  }
})
